﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FastMVVM.Silverlight.Template.ViewModel;
using FastMVVM.Core;

namespace FastMVVM.Silverlight.Template.View
{
    public partial class MainPageView : UserControl, IView<MainPageViewModel>
    {
        // The ViewModel should be the logic layer.
        // Use XAML Binding to access his properties.
        public MainPageViewModel ViewModel { get; set; }

        // The order of the calls need to be preservated.
        // Do not add any additional code to this, unless you really know what are you doing.
        // Any other initilization logic should be at the "UserControl_Loaded".
        public MainPageView()
        {
            ViewModel = ViewModelFactory.New<MainPageViewModel>();
            // if you want to share the same ViewModel with diferent Views change to:
            //ViewModel = ViewModelFactory.GetInstance<MainPageViewModel>();

            SetViewModelEventHandlers();
            
            InitializeComponent();
        }

        // Occurs only after all the XAML's controls have been created.
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = ViewModel;

            ViewModel.LoadMessage();
        }

        // Use to set the event handler to all the ViewModel's events.
        public void SetViewModelEventHandlers()
        {
            ViewModel.ShowMessage += ViewModel_ShowMessage;
        }

        private void ViewModel_ShowMessage(object sender, EventArgs e)
        {
            sbShowMessage.Begin();
        }
    }
}
